package com.darwin.darwinUI.manager
{
	/**
	 * 语言管理器
	 * @author Roland
	 */
	public class LanguageManager
	{
		public static const XML_TYPE:String = "xml";
		/**
		 * 直接键值对象列表 
		 */
		public static const JSON_TYPE:String = "json";
		public function LanguageManager()
		{
			
		}
		
		
		private var textDic:Object = {};
		
		
		/**
		 * 加入语言文字库XML
		 * @param resource
		 */
		public function initialize(resource:*,type:String = "xml"):void	{
			if(resource)	{
				if(type == LanguageManager.XML_TYPE)
				{
					var xml:XML;
					if(resource is XML)	{
						xml = resource as XML;
					}else	{
						xml = new XML(resource.toString());
					}
					var xmlList:XMLList = xml..r;
					for each(var child:XML in xmlList)
					{
						var $name:String = child.@name;
						var $text:String = child[0];
						textDic[$name] = $text;
					}
				}
			}
		}
		
		
		/**
		 *  取得一个文本
		 * @param resourceName 文字索引，如果库中找不到这个索引则直接使用resourceName
		 * @param parameters 替换的参数，用于替换{}中的内容
		 * @return 
		 */
		public function getText(resourceName:String,parameters:Array = null):String
		{
			var resourceStr:String;
			if(textDic[resourceName] != null)
			{
				resourceStr =  textDic[resourceName]
			}else{
				resourceStr = resourceName;
			}
			if (parameters)	{
				for (var j:int = 0, pLen:int = parameters.length; j < pLen; j++)	{
					var placeStr:String = "\\{" + j + "\\}";
					var re:RegExp = new RegExp(placeStr,"g");
					resourceStr = resourceStr.replace(re, parameters[j]);
				}
			}
			
			return resourceStr;
			
			
		}
		
	}
}